package game.games.gol;

import game.console.AbstractIteratorConsoleStream;
import game.console.IConsoleCommand;
import game.console.IConsoleStream;
import game.console.MessageConsoleStream;

import java.util.ArrayList;
import java.util.List;

public class GameOfLifeCommands
{
    public static List<IConsoleCommand> getCommands(final GameOfLife gol)
    {
        // pause
        List<IConsoleCommand> commands = new ArrayList<IConsoleCommand>();
        commands.add(new IConsoleCommand() {
            @Override
            public String getCommand()
            {
                return "pause";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "pause game - 'pause'";
            }
            
            @Override
            public IConsoleStream fireCommand(String[] args)
            {
                gol.pause();
                return new MessageConsoleStream("Pause = " + gol.isPaused());
            }
        });
        // print rules
        commands.add(new IConsoleCommand() {
            
            @Override
            public String getCommand()
            {
                return "rules";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "list some example rules";
            }
            
            @Override
            public IConsoleStream fireCommand(String[] args)
            {
                return new AbstractIteratorConsoleStream<GameOfLifeRule>() {
                    
                    @Override
                    protected GameOfLifeRule[] getValues()
                    {
                        return GameOfLifeRule.getRules();
                    }
                    
                    @Override
                    protected String getMessage(GameOfLifeRule current)
                    {
                        return current.getRule();
                    }
                };
            }
        });
        // load rule
        commands.add(new IConsoleCommand() {
            @Override
            public String getCommand()
            {
                return "rule";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "change gol rule, param is gol-rule-syntax SURVIVE/BIRTH - 'rule 23/3'";
            }
            
            @Override
            public IConsoleStream fireCommand(String[] args)
            {
                gol.setRule(args[1]);
                return new MessageConsoleStream("NEW RULE: '" + args[1] + "'");
            }
        });
        // load rule and invert
        commands.add(new IConsoleCommand() {
            @Override
            public String getCommand()
            {
                return "irule";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "change gol rule, param is gol-rule-syntax SURVIVE/BIRTH - 'rule 23/3'";
            }
            
            @Override
            public IConsoleStream fireCommand(String[] args)
            {
                GameOfLifeRule golRule = new GameOfLifeRule(args[1]);
                golRule.invert();
                gol.setRule(0, golRule);
                return new MessageConsoleStream("NEW RULE: '" + args[1] + "'");
            }
        });
        commands.add(new IConsoleCommand() {
            @Override
            public String getCommand()
            {
                return "color";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "change color, available colors: 0 = (green, red, black), 1 = (black, white)";
            }
            
            @Override
            public IConsoleStream fireCommand(String[] args)
            {
                gol.setColorSet(ColorSet.getById(args[1]));
                return new MessageConsoleStream("NEW COLOR: '" + args[1] + "'");
            }
        });
        commands.add(new IConsoleCommand() {
            @Override
            public String getCommand()
            {
                return "lc%";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "change living cells % of total cells on next restart, WARNING performance decreases extremely when % is greater than 50";
            }
            
            @Override
            public IConsoleStream fireCommand(String[] args)
            {
                if (args.length == 1)
                {
                    return new MessageConsoleStream("$1 required");
                }
                else
                {
                    int prozent = Integer.parseInt(args[1]);
                    gol.setProzentLivingCells(prozent);
                    return new MessageConsoleStream("LIVING CELLS ON RESTART: " + prozent + " %");
                }
            }
        });
        commands.add(new IConsoleCommand() {
            @Override
            public String getCommand()
            {
                return "lc";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "change living total cells on next restart";
            }
            
            @Override
            public IConsoleStream fireCommand(String[] args)
            {
                int living = Integer.parseInt(args[1]);
                gol.setNextLivingCells(living);
                return new MessageConsoleStream("LIVING CELLS ON RESTART: " + living);
            }
        });
        // zone rule
        commands.add(new IConsoleCommand()
        {
            
            @Override
            public String[] getSynonym()
            {
                return new String[] { "zr" };
            }
            
            @Override
            public String getInfo()
            {
                return "zone rules";
            }
            
            @Override
            public String getCommand()
            {
                return "zrule";
            }
            
            @Override
            public IConsoleStream fireCommand(String[] args)
            {
                String zone = args[1];
                String rule = args[2];
                gol.setRule(Integer.parseInt(zone), rule);
                return new MessageConsoleStream("zone " + zone + " rule created: " + rule);
            }
        });
        return commands;
    }
}
